<template>
  <div class="gl_contentBox gl_heightFull">
    <div class="demo-input-suffix gl_searchBox">
      <el-row :gutter="20">
        <el-col :md="4" :lg="3">
          <p>快递名称</p>
          <el-input v-model="selectedMsg.Filter" placeholder="请输入" size="mini" clearable @change="getPageList()" />
        </el-col>
        <el-col :md="20" :lg="21" :xl="21" style="text-align:right">
          <!-- 查询按钮 -->
          <el-button style="margin-top:27px" type="primary" size="mini" @click="getPageList()">{{ $t('button.query') }}</el-button>
          <!-- 重置按钮 -->
          <el-button style="margin-top:27px" type="warning" size="mini" @click="clickReset">{{ $t('button.Reset') }}</el-button>
        </el-col>
      </el-row>
    </div>
    <template>
      <el-row style="margin:15px 0 8px 0">
        <el-col :md="6" :lg="4">
          <el-button type="primary" size="mini" icon="el-icon-circle-plus-outline" @click="clickAdd()">添加快递</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="tableLoading" :data="tableData" class="gl_table_noBorder" size="mini" style="width: 100%;">
        <el-table-column
          label="快递名称"
        >
          <template slot-scope="{row}">
            {{ row.name || '--' }}
          </template>
        </el-table-column>
        <el-table-column
          label="创建人"
        >
          <template slot-scope="{row}">
            {{ row.creatorName || '--' }}
          </template>
        </el-table-column>
        <!-- 创建时间 -->
        <el-table-column
          label="创建时间"
        >
          <template slot-scope="{row}">
            <span v-if="row.creationTime">
              {{ row.creationTime | FormatDate('yyyy-MM-dd HH:mm:ss') }}
            </span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('table.actions')" width="160">
          <template slot-scope="{ row }">
            <el-button size="mini" type="text" @click="clickEdit(row)">编辑</el-button>
            <el-button size="mini" type="text" style="color:#FF3434" @click="clickDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row class="gl_pt_20" style="text-align: right;">
        <el-pagination
          :current-page="selectedMsg.Page"
          :page-size="selectedMsg.MaxResultCount"
          :total="totalPage"
          layout="total,sizes,prev,pager,next,jumper"
          :page-sizes="[10, 20, 30, 40]"
          @size-change="sizeChange"
          @current-change="pageIndexChange"
        />
      </el-row>
    </template>
    <edit-express ref="editExpress" :store-id="storeId" />
  </div>
</template>

<script>
import editExpress from './editExpress';
import { Api_GET_MallStoreExpressQO } from '@/lib/form/shoppingmall/swaggerForm';
import {
  api_GET_MallStoreCurrent,
  api_GET_MallStoreExpress,
  api_GET_MallStoreExpressId,
  api_DELETE_MallStoreExpressId
} from '@/extend/api/shoppingmallswaggerApi/app';
export default {
  components: {
    editExpress
  },
  data() {
    return {
      selectedMsg: new Api_GET_MallStoreExpressQO(),
      tableLoading: false,
      totalPage: 0,
      tableData: [],
      storeId: ''
    };
  },
  watch: {
    'storeId': {
      handler: function(val) {
        if (!val) return;
        this.$set(this.selectedMsg, 'StoreId', val);
        this.getPageList();
      }
    }
  },
  created() {
    this.api_GET_MallStoreCurrent();
  },
  methods: {
    // 获取当前用户店铺信息
    api_GET_MallStoreCurrent() {
      api_GET_MallStoreCurrent({ IncludeStatus: true }).then(res => {
        this.storeId = res.id || '';
      }, error => {
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    // 获取快递列表
    getPageList(page) {
      if (this.selectedMsg.StoreId === '') return;
      this.tableLoading = true;
      this.selectedMsg.Page = page || 1;
      this.selectedMsg.SkipCount = (this.selectedMsg.Page - 1) * this.selectedMsg.MaxResultCount;
      api_GET_MallStoreExpress(this.selectedMsg).then(res => {
        this.tableLoading = false;
        this.tableData = res.items;
        this.totalPage = res.totalCount;
      }, error => {
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    clickAdd() {
      this.$refs.editExpress.dialogVisible = true;
      this.$refs.editExpress.type = 'add';
    },
    clickEdit(row) {
      this.$refs.editExpress.loading = true;
      this.$refs.editExpress.dialogVisible = true;
      this.$refs.editExpress.type = 'edit';
      this.$refs.editExpress.rowId = row.id;
      api_GET_MallStoreExpressId(row.id).then(res => {
        this.$refs.editExpress.selectedMsg.name = res.name;
        this.$refs.editExpress.loading = false;
      }, error => {
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    clickDelete(row) {
      this.$confirm(`您确定要删除吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          api_DELETE_MallStoreExpressId(row.id).then(res => {
            this.$message({ type: 'success', message: '删除成功' });
            this.getPageList();
          }, error => {
            const _msg = error.error.message || error.message;
            this.$message({ type: 'error', message: _msg });
          });
        })
        .catch(() => {
        });
    },
    clickReset() {
      this.selectedMsg = new Api_GET_MallStoreExpressQO();
      this.selectedMsg.StoreId = this.storeId;
      this.getPageList();
    },
    // 分页
    sizeChange(MaxResultCount) {
      this.selectedMsg.MaxResultCount = MaxResultCount;
      this.getPageList();
    },
    pageIndexChange(currentPage) {
      this.getPageList(currentPage);
    }
  }
};
</script>
<style lang="scss" scoped>
</style>
